package com.xnf.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnf.behavior.mapper.ApUnlikesBehaviorMapper;
import com.xnf.behavior.service.ApUnlikesBehaviorService;
import com.xnf.common.message.HotArticleConstants;
import com.xnf.model.behavior.dtos.UnLikesBehaviorDto;
import com.xnf.model.behavior.pojos.ApBehaviorEntry;
import com.xnf.model.behavior.pojos.ApUnlikesBehavior;
import com.xnf.model.common.dtos.ResponseResult;
import com.xnf.model.common.enums.AppHttpCodeEnum;
import com.xnf.model.mess.app.UpdateArticleMess;
import com.xnf.model.user.pojos.ApUser;
import com.xnf.utils.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * Created by xnf on 2022/3/8.
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper,ApUnlikesBehavior> implements ApUnlikesBehaviorService{

    @Autowired
    private ApBehaviorEntryServiceImpl apBehaviorEntryService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {
        if (dto == null || dto.getType() < 0 || dto.getType() > 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser apUser = AppThreadLocalUtils.get();
        if (apUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(apUser.getId(), null);
        if (apBehaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUnlikesBehavior apUnlikesBehavior = getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery()
                                                            .eq(ApUnlikesBehavior::getEntryId, apBehaviorEntry.getEntryId())
                                                            .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId()));
        if (apUnlikesBehavior == null && dto.getType() == 0){
            apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setType(Integer.valueOf(dto.getType()));
            apUnlikesBehavior.setCreatedTime(new Date());
            apUnlikesBehavior.setEntryId(apBehaviorEntry.getEntryId());
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            save(apUnlikesBehavior);
        }else{
            System.out.println(apUnlikesBehavior);
            apUnlikesBehavior.setType(Integer.valueOf(dto.getType()));
            updateById(apUnlikesBehavior);
        }
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setAdd(-1);
        mess.setType(UpdateArticleMess.UpdateArticleType.UNLIKE);
        mess.setArticleId(apUnlikesBehavior.getArticleId());
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, UUID.randomUUID().toString(), JSON.toJSONString(mess));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
